/*
 * MainResultWindowGui.java
 *
 * Created on April 23, 2008, 10:38 AM
 */
package gui;

import baseLib.SysApoio;
import baseLib.SysProperties;
import business.facades.WorldFacade;
import control.MapaControler;
import control.WorldControler;
import gui.tabs.TabPersonagensGui;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.io.Serializable;
import javax.swing.JButton;
import javax.swing.plaf.basic.BasicSplitPaneUI;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import persistence.BundleManager;
import persistence.SettingsManager;

/**
 *
 * @author gurgel
 */
public class MainResultWindowGui extends javax.swing.JPanel implements Serializable {

    private static final Log log = LogFactory.getLog(MainResultWindowGui.class);
    private static final BundleManager labels = SettingsManager.getInstance().getBundleManager();
    private WorldControler wc = new WorldControler(this);
    private TabPersonagensGui tabPersonagem;

    /**
     * Creates new form MainResultWindowGui
     */
    public MainResultWindowGui(String autoLoad) {
        initComponents();
        //adiciona listeners
        jbOpen.addActionListener(wc);
        jbLoad.addActionListener(wc);
        jbSave.addActionListener(wc);
        jbSaveWorld.addActionListener(wc);
        jbCopy.addActionListener(wc);
        jbSend.addActionListener(wc);
        jbAbout.addActionListener(wc);
        wc.doAutoLoad(autoLoad);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        infoPanel = new javax.swing.JPanel();
        labelNacao = new javax.swing.JLabel();
        labelMoney = new javax.swing.JLabel();
        labelJogador = new javax.swing.JLabel();
        labelCenario = new javax.swing.JLabel();
        labelTurno = new javax.swing.JLabel();
        labelDeadline = new javax.swing.JLabel();
        labelJogo = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        jbOpen = new javax.swing.JButton();
        jbLoad = new javax.swing.JButton();
        jbSaveWorld = new javax.swing.JButton();
        jbSave = new javax.swing.JButton();
        jbCopy = new javax.swing.JButton();
        jbSend = new javax.swing.JButton();
        jbAbout = new javax.swing.JButton();
        splitMainPanel = new javax.swing.JSplitPane();
        jlLeft = new javax.swing.JLabel();
        jlRight = new javax.swing.JLabel();
        statusBar = new javax.swing.JPanel();
        statusLabel = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(1024, 768));

        infoPanel.setBackground(new java.awt.Color(204, 204, 255));

        labelNacao.setText(labels.getString("NACAO")); // NOI18N

        labelMoney.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("labels"); // NOI18N
        labelMoney.setText(bundle.getString("MONEY")); // NOI18N
        labelMoney.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        labelJogador.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        labelJogador.setText(labels.getString("JOGADOR")); // NOI18N
        labelJogador.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        labelCenario.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelCenario.setText(labels.getString("CENARIO")); // NOI18N
        labelCenario.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        labelTurno.setText(labels.getString("TURNO")); // NOI18N
        labelTurno.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);

        labelDeadline.setText(labels.getString("TURNO")); // NOI18N
        labelDeadline.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);

        labelJogo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelJogo.setText(labels.getString("JOGO")); // NOI18N
        labelJogo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        jbOpen.setText(labels.getString("ABRIR.TURNO")); // NOI18N
        jbOpen.setToolTipText(labels.getString("ABRIR.TURNO.TOOLTIP")); // NOI18N
        jbOpen.setActionCommand("jbOpen");
        jbOpen.setFocusable(false);
        jbOpen.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbOpen.setName("jbOpen"); // NOI18N
        jbOpen.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jbOpen);

        jbLoad.setText(labels.getString("CARREGAR.ACOES")); // NOI18N
        jbLoad.setToolTipText(labels.getString("CARREGAR.ACOES.TOOLTIP")); // NOI18N
        jbLoad.setActionCommand("jbLoad");
        jbLoad.setEnabled(false);
        jbLoad.setFocusable(false);
        jbLoad.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbLoad.setName("jbLoad"); // NOI18N
        jbLoad.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jbLoad);

        jbSaveWorld.setText(labels.getString("SALVAR.WORLD")); // NOI18N
        jbSaveWorld.setToolTipText(labels.getString("SALVAR.WORLD.TOOLTIP")); // NOI18N
        jbSaveWorld.setActionCommand("jbSaveWorld");
        jbSaveWorld.setEnabled(false);
        jbSaveWorld.setFocusable(false);
        jbSaveWorld.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbSaveWorld.setName("jbSaveWorld"); // NOI18N
        jbSaveWorld.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jbSaveWorld);

        jbSave.setText(labels.getString("SALVAR.ACOES")); // NOI18N
        jbSave.setToolTipText(labels.getString("SALVAR.ACOES.TOOLTIP")); // NOI18N
        jbSave.setActionCommand("jbSave");
        jbSave.setEnabled(false);
        jbSave.setFocusable(false);
        jbSave.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbSave.setName("jbSave"); // NOI18N
        jbSave.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jbSave);

        jbCopy.setText(labels.getString("COPIAR.ACOES")); // NOI18N
        jbCopy.setToolTipText(labels.getString("COPIAR.ACOES.TOOLTIP")); // NOI18N
        jbCopy.setActionCommand("jbCopy");
        jbCopy.setEnabled(false);
        jbCopy.setFocusable(false);
        jbCopy.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbCopy.setName("jbCopy"); // NOI18N
        jbCopy.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jbCopy);

        jbSend.setText(labels.getString("ENVIAR.ACOES")); // NOI18N
        jbSend.setToolTipText(labels.getString("ENVIAR.ACOES.TOOLTIP")); // NOI18N
        jbSend.setActionCommand("jbSend");
        jbSend.setEnabled(false);
        jbSend.setFocusable(false);
        jbSend.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbSend.setName("jbSend"); // NOI18N
        jbSend.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jbSend);

        jbAbout.setText(labels.getString("MENU.ABOUT")); // NOI18N
        jbAbout.setToolTipText(labels.getString("SALVAR.ACOES.TOOLTIP")); // NOI18N
        jbAbout.setActionCommand("jbAbout");
        jbAbout.setFocusable(false);
        jbAbout.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbAbout.setName("jbAbout"); // NOI18N
        jbAbout.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jbAbout);

        javax.swing.GroupLayout infoPanelLayout = new javax.swing.GroupLayout(infoPanel);
        infoPanel.setLayout(infoPanelLayout);
        infoPanelLayout.setHorizontalGroup(
            infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(infoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(infoPanelLayout.createSequentialGroup()
                        .addComponent(labelTurno, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(15, 15, 15)
                        .addComponent(labelDeadline, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(infoPanelLayout.createSequentialGroup()
                        .addComponent(labelNacao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(3, 3, 3)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelJogo, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE)
                    .addComponent(labelCenario, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelMoney, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
                    .addComponent(labelJogador, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)))
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 782, Short.MAX_VALUE)
        );
        infoPanelLayout.setVerticalGroup(
            infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(infoPanelLayout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(labelJogo)
                        .addComponent(labelNacao))
                    .addComponent(labelJogador))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCenario)
                    .addComponent(labelMoney)
                    .addComponent(labelTurno)
                    .addComponent(labelDeadline))
                .addContainerGap())
        );

        splitMainPanel.setDividerSize(9);
        splitMainPanel.setAutoscrolls(true);
        splitMainPanel.setMinimumSize(new java.awt.Dimension(0, 0));
        splitMainPanel.setOneTouchExpandable(true);

        jlLeft.setToolTipText("Open.");
        splitMainPanel.setLeftComponent(jlLeft);
        splitMainPanel.setRightComponent(jlRight);

        statusBar.setBackground(new java.awt.Color(204, 204, 255));
        statusBar.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        statusLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusLabel.setText(labels.getString("STATUS.MESSAGES")); // NOI18N

        javax.swing.GroupLayout statusBarLayout = new javax.swing.GroupLayout(statusBar);
        statusBar.setLayout(statusBarLayout);
        statusBarLayout.setHorizontalGroup(
            statusBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 1010, Short.MAX_VALUE)
        );
        statusBarLayout.setVerticalGroup(
            statusBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusLabel)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(infoPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(statusBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(splitMainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 1024, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(infoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(splitMainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 662, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel infoPanel;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton jbAbout;
    private javax.swing.JButton jbCopy;
    private javax.swing.JButton jbLoad;
    private javax.swing.JButton jbOpen;
    private javax.swing.JButton jbSave;
    private javax.swing.JButton jbSaveWorld;
    private javax.swing.JButton jbSend;
    private javax.swing.JLabel jlLeft;
    private javax.swing.JLabel jlRight;
    private javax.swing.JLabel labelCenario;
    private javax.swing.JLabel labelDeadline;
    private javax.swing.JLabel labelJogador;
    private javax.swing.JLabel labelJogo;
    private javax.swing.JLabel labelMoney;
    private javax.swing.JLabel labelNacao;
    private javax.swing.JLabel labelTurno;
    private javax.swing.JSplitPane splitMainPanel;
    private javax.swing.JPanel statusBar;
    private javax.swing.JLabel statusLabel;
    // End of variables declaration//GEN-END:variables

    public void iniciaConfig() {
        log.info(labels.getString("INICIALIZANDO.GUI"));
        //infopanel
        this.labelCenario.setText(String.format("%s: %s", labels.getString("CENARIO"), wc.getCenarioNome()));
        if (wc.isGameOver()) {
            this.labelDeadline.setText(String.format(labels.getString("GAMEOVER"), wc.getTurno()));
            this.labelDeadline.setForeground(Color.white);
            this.infoPanel.setBackground(Color.orange);
        } else if (wc.isJogadorAtivoEliminado()) {
            this.labelDeadline.setText(String.format(labels.getString("GAMEOVER.PLAYER"), wc.getTurno()));
            this.labelDeadline.setForeground(Color.white);
            this.infoPanel.setBackground(Color.orange);
        } else {
            if (wc.getDeadlineDaysRemaining() > 0) {
                this.labelDeadline.setText(String.format(labels.getString("PARTIDA.LABEL.DEADLINE"), wc.getDeadlineDaysRemaining(), wc.getDeadline()));
                this.labelDeadline.setForeground(Color.black);
                this.infoPanel.setBackground(new Color(204, 204, 255));
            } else {
                this.labelDeadline.setText(String.format(labels.getString("PARTIDA.LABEL.DEADLINE.NOW"), wc.getDeadline()));
                this.labelDeadline.setForeground(Color.white);
                this.infoPanel.setBackground(Color.orange);
            }
            this.infoPanel.repaint();
        }
        this.labelTurno.setText(String.format("%s: %s", labels.getString("TURNO"), wc.getTurno()));
        this.labelJogador.setText(String.format("%s: %s", labels.getString("JOGADOR"), wc.getJogadorAtivoNome()));
        this.labelMoney.setText(String.format("%s: %s", labels.getString("MONEY"), wc.getJogadorAtivoNome()));
        this.labelJogo.setText(String.format("%s: %s", labels.getString("JOGO"), wc.getPartidaNome()));
        if (wc.getNacoesJogadorAtivoQtd() < 4) {
            this.labelNacao.setText(String.format("%s: %s", labels.getString("NACAO"), wc.getNacoesJogadorAtivoNome()));
        } else {
            this.labelNacao.setText(String.format("%s: %s", labels.getString("NACAO"), wc.getNacoesJogadorAtivoQtd() + " ... "));
        }

        MapaControler mapaControl = new MapaControler(this);
        WorldFacade.getInstance().setMapaControler(mapaControl);
        MainMapaGui mapaGui = new MainMapaGui(mapaControl);
        //Monta área do mapa
        this.splitMainPanel.setRightComponent(mapaGui);
        // Monta tabbed panel para dados
        MainDadosGui dadosGui = new MainDadosGui(mapaControl);
        this.splitMainPanel.setLeftComponent(dadosGui);
        this.tabPersonagem = dadosGui.getTabPersonagem();

        //habilita o toolbar
        jbLoad.setEnabled(true);
        jbSave.setEnabled(true);
        if (SettingsManager.getInstance().isWorldBuilder()) {
            jbSaveWorld.setEnabled(true);
        } else {
            jbSaveWorld.setEnabled(false);
            jbSaveWorld.setVisible(false);
        }
        jbCopy.setEnabled(true);
        jbSend.setEnabled(true);
        jbAbout.setEnabled(true);
        // Joga a janela toda para a esquerda = 0
        // divide a janela ao meio = -1
        //        this.splitMainPanel.setDividerLocation(55555);
        int splitWid = SysApoio.parseInt(SysProperties.getProps("splitSize", "660"));
        this.splitMainPanel.setDividerLocation(splitWid);
        doMinimizeMap();
    }

    public void doMinimizeMap() {
        if (SysProperties.getProps("minimizeMapOnStart", "0").equals("1")) {
            //configuracoes para o autoloader
            this.splitMainPanel.addComponentListener(new ComponentListener() {
                @Override
                public void componentHidden(ComponentEvent e) {
                }

                @Override
                public void componentMoved(ComponentEvent e) {
                }

                @Override
                public void componentResized(ComponentEvent e) {
                    splitMainPanel.setDividerLocation(1.0);
                }

                @Override
                public void componentShown(ComponentEvent e) {
                    splitMainPanel.setDividerLocation(1.0);
                }
            });
            //configuracoes caso esteja abvrindo outro turno com a interface ja exibida.
            //forcando atualizacao das labels para nao alterar oe stado do splitpanel
            this.validate();
            this.setVisible(true);
            BasicSplitPaneUI uiTest = (BasicSplitPaneUI) this.splitMainPanel.getUI();
            Container divider = uiTest.getDivider();
            JButton max = (JButton) divider.getComponent(1);
            max.doClick();
        }
    }

    public TabPersonagensGui getTabPersonagem() {
        return this.tabPersonagem;
    }

    public void setStatusMsg(String txt) {
        statusLabel.setText(txt);
    }

    public void setLabelMoney(String txt) {
        if (SysApoio.parseInt(txt) > 0) {
            this.labelMoney.setForeground(Color.black);
        } else {
            this.labelMoney.setForeground(Color.red);
        }
        this.labelMoney.setText(String.format("%s %s", txt, labels.getString("MONEY")));
    }
}
